#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int n = nums.size();

        int left = 0;
        int right = n - 1;
        
        while (left <= right) 
        {
            int mid = (right - left) / 2 + left;
            if (nums[mid] == target) 
                return mid;

            if (nums[0] <= nums[mid]) 
            {
                //mid的左面处于旋转的区域
                //判断target是否在该区域
                if (nums[0] <= target && target < nums[mid]) 
                {
                    right = mid - 1;
                } 
                else 
                {
                    left = mid + 1;
                }
            } 
            else 
            {
                //mid的右边是未旋转的区域
                //判断target是否在未旋转区域
                if (nums[mid] < target && target <= nums[n - 1]) 
                {
                    left = mid + 1;
                } 
                else 
                {
                    right = mid - 1;
                }
            }
        }
        return -1;
    }
};

//..